﻿#region 참조 네임스페이스

using Skying.Net;
using System;
using System.Text;
using System.Windows.Forms;

#endregion

namespace AsyncHttpRequestBrowser
{
	public partial class MainForm : Form
	{
		#region 생성자/소멸자

		public MainForm()
		{
			this.InitializeComponent();

			this.UriComboBox.Text = "http://www.google.com";
			this.TypeComboBox.SelectedIndex = 0;
		}

		#endregion

		#region 패널 이벤트

		/// <summary>
		/// Uri 콤보박스의 패널이 리사이즈됐을 때 발생하는 이벤트를 구현한다.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void panel2_Resize(object sender, EventArgs e)
		{
			Panel panel = sender as Panel;

			this.UriComboBox.Width = panel.Width - panel.Padding.Left - panel.Padding.Right;
		}

		#endregion

		#region 버튼 이벤트

		/// <summary>
		/// Request 버튼이 클릭됐을 때 발생하는 이벤트를 구현한다.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void RequestButton_Click(object sender, EventArgs e)
		{
			this.DocumentTextBox.Text = string.Empty;

			switch (this.TypeComboBox.SelectedIndex)
			{
				case 0:

					HttpRequest request = new HttpRequest();

					request.Responsed = (eventArgs) =>
					{
						this.DocumentTextBox.Text = eventArgs.Result.DocumentText;
					};

					request.Request(this.UriComboBox.Text);

					break;

				case 1:

					WebClient client = new WebClient(300);

					//client.DownloadDataCompleted += (s, eventArgs) =>
					//{
					//	this.DocumentTextBox.Text += Encoding.UTF8.GetString(eventArgs.Result);
					//};

					//client.DownloadDataAsync(new Uri(this.UriComboBox.Text));
					
					client.DownloadStringCompleted += (s, eventArgs) =>
					{
						this.DocumentTextBox.Text = eventArgs.Result;
					};

					client.DownloadStringAsync(new Uri(this.UriComboBox.Text));

					break;
			}
		}

		#endregion
	}
}